package com.touzhijia.authority.controller;

import com.github.pagehelper.PageInfo;
import com.touzhijia.authority.domain.dto.request.*;
import com.touzhijia.authority.domain.dto.response.RoleByUserRes;
import com.touzhijia.authority.domain.dto.response.RolePermissionAddRes;
import com.touzhijia.authority.domain.entity.Role;
import com.touzhijia.authority.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Api(description = "角色管理")
@RestController
@RequestMapping("/api")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation("获取角色信息")
    @GetMapping("/role/{name}")
    public Role getRole(@PathVariable("name") String name) {
        return roleService.getRole(name);
    }

    @ApiOperation("查询角色信息")
    @GetMapping("/role")
    public PageInfo<Role> findRole(@Validated RoleQueryReq queryReq) {
        return roleService.findRole(queryReq);
    }

    @ApiOperation("新增角色信息")
    @PostMapping("/role")
    public Role addRole(@Validated @RequestBody RoleAddReq roleAddReq) {
        return roleService.addRole(roleAddReq);
    }

    @ApiOperation("删除角色信息")
    @DeleteMapping("/role/{name}")
    public Role delRole(@PathVariable("name") String name) {
        return roleService.delRole(name);
    }

    @ApiOperation("修改角色信息")
    @PutMapping("/role/{name}")
    public Role updateRole(@PathVariable("name") String name, @Validated @RequestBody RoleUpdateReq updateReq) {
        return roleService.updateRole(name, updateReq);
    }

    @ApiOperation("新增角色权限")
    @PostMapping("/role/permission")
    public RolePermissionAddRes addRolePermission(@Validated @RequestBody RolePermissionAddReq addReq) {
        return roleService.addRolePermission(addReq);
    }

    @ApiOperation("删除角色权限")
    @DeleteMapping("/role/permission/{roleName}/{permission}")
    public RolePermissionAddRes removeRolePermission(@PathVariable("roleName") String roleName, @PathVariable("permission") String permission) {
        return roleService.removeRolePermission(roleName, permission);
    }

    @ApiOperation("根据用户查询角色信息")
    @GetMapping("/role/user")
    public PageInfo<RoleByUserRes> findRoleByUser(@Validated RoleQueryByUserReq queryReq) {
        return roleService.findRoleByUser(queryReq);
    }
}
